// pages/treasurebox/weight.js

const db = wx.cloud.database()

const _ = db.command
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '',
    showbox: false, //
    show: false, //选择日期隐藏
    isshow: false, //有数据显示
    isrecord: true, //记录
    dataList: [], //集合数组
    date: "",//日期
    username3: "",//体重
    radio: "name",//睡前后
    counterId: '',
    openid: '',
    count: null,


  },

  //添加获取
  addData() {
    let that = this
    var date1=this.data.date
    var reg=/\d/
    var username3=this.data.username3
    var radio= this.data.radio
   if(date1==''|| username3==' '|| radio==''&& username3==/\d/){
    wx.showToast({
      title: '请补全',
    })
   }else{
    db.collection('user_data').add({
      data: {
        date: this.data.date,
        username3: this.data.username3,
        radio: this.data.radio
      },
      success:(res) =>{
        db.collection('user_data').get({
          success: (res) => {
            this.setData({
              dataList: res.data,
              showbox:false,
              isshow:false
            })
            console.log(res)
          }
        })
      },
      fail(res) {
        console.log("失败", res);
      }
    })
   }
 
    db.collection('user_data').get({
      success: function (res) {
        that.setData({
          dataList: res.data,
        
        })

        console.log(res.data.dataList)
      }
    })

  },
  //点击删除
  delData(e) {
 
    let delId=this.data.dataList[e.currentTarget.dataset.index]._id
    var that=this
    db.collection('user_data').doc(delId).remove({
      success: function(res) {
        console.log(res.data)
      },
      addData(){
        db.collection('user_data').get()
          then (res=> {
            that.setData({
              dataList: res.data,
              isshow:res.data.length>0?true:false
            })
          })

      }
          })
 

  
   
  

  },
  //点击添加弹框
  showPopup() {
    this.setData({
      showbox: true
    });
  },
  onClose() {
    this.setData({
      showbox: false
    });
  },
  //日期
  onDisplay() {
    this.setData({
      show: true
    });
  },
  onClose1() {
    this.setData({
      show: false
    });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear() + 1}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail),
    });
  },

  //页面加载显示
  onLoad: function (options) {

    db.collection('user_data').get({
      success: (res) => {
        this.setData({
          dataList: res.data,
          isshow:res.data.length <= 0 ? true:false
        })
        var showDataList=[];
        outer:
         for(let i=0;i<dataList.length;i++){
          var username3=dataList[i]; //一条条数据
          for (let j = 0; j < showDataList.length; j++) {
            var showusername3 = showDataList[j];
            if (username3.date==showusername3.date) {
            showusername3.typeDataList.push({
              id:username3.id,
              username3:username3.username3,
              radio:username3.radio
            })
            continue outer;
            }
          }
         }
        //不存在时的整个对象
        showDataList.push({
          date:username3.date,
          typeDataList:[{
            id:username3.id,
            username3:username3.username3,
            radio:username3.radio
          }]
        })

        console.log('111',res)

      },
     
     
    })
    //绑定openid
    if (app.globalData.openid) {
      this.setData({
        openid: app.globalData.openid
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})